package net.qiqbframework.modelling.command;

import net.qiqbframework.commandhandling.CommandMessage;

import java.util.ArrayList;
import java.util.List;

public class MultiAggregateHandlingMonitor implements AggregateHandlingMonitor {

    private final List<AggregateHandlingMonitor> messageMonitors;

    public MultiAggregateHandlingMonitor() {
        messageMonitors = new ArrayList<>();
    }

    public MultiAggregateHandlingMonitor(List<AggregateHandlingMonitor> messageMonitors) {
        this.messageMonitors = new ArrayList<>(messageMonitors);
    }

    public void addMessageMonitor(AggregateHandlingMonitor messageMonitor) {
        messageMonitors.add(messageMonitor);
    }

    @Override
    public <T> void beforeHandle(CommandMessage<?> commandMessage, T beforeHandleAggregateRoot) {
        for (AggregateHandlingMonitor messageMonitor : messageMonitors) {
            messageMonitor.beforeHandle(commandMessage, beforeHandleAggregateRoot);
        }
    }

    @Override
    public <T> void afterCommit(CommandMessage<?> commandMessage, T beforeHandleAggregateRoot, T afterHandleAggregateRoot) {
        for (AggregateHandlingMonitor messageMonitor : messageMonitors) {
            messageMonitor.afterCommit(commandMessage, beforeHandleAggregateRoot, afterHandleAggregateRoot);
        }
    }


}
